package edu.nwpu.service.impl;

import edu.nwpu.dao.RecvInfoMapper;
import edu.nwpu.pojo.RecvInfo;
import edu.nwpu.service.RecvInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("recvInfoService")
public class RecvInfoServiceImpl implements RecvInfoService {

    @Resource
    private RecvInfoMapper recvInfoMapper;

    @Override
    public boolean addRecvInfo(RecvInfo recvInfo) {
        if (recvInfo.getAddress() == null || "".equals(recvInfo.getAddress())
            || recvInfo.getName() == null || "".equals(recvInfo.getName())
            || recvInfo.getPhoneNo() == null || "".equals(recvInfo.getPhoneNo())){
            return false;
        }
        return recvInfoMapper.insertRecvInfo(recvInfo) > 0;
    }

    @Override
    public boolean deleteRecvInfoById(long id) {
        return recvInfoMapper.updateStatusCodeById(id, 0) == 1;
    }

    @Override
    public boolean updateRecvInfo(RecvInfo recvInfo) {
        if (recvInfo.getAddress() == null || "".equals(recvInfo.getAddress())
                || recvInfo.getName() == null || "".equals(recvInfo.getName())
                || recvInfo.getPhoneNo() == null || "".equals(recvInfo.getPhoneNo())){
            return false;
        }
        if (recvInfoMapper.selectByPrimaryKey(recvInfo.getId()) == null){
            return false;
        }
        return recvInfoMapper.updateByPrimaryKey(recvInfo) > 0;
    }

    @Override
    public List<RecvInfo> getRecvInfoByUserId(long userId) {
        return recvInfoMapper.selectByUserId(userId);
    }
}
